package org.example.common.sensitive.core;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;

import java.util.function.Function;

/**
 * 脱敏策略
 *
 */
@AllArgsConstructor
public enum SensitiveStrategy {

    /**
     * 身份证脱敏
     */
    ID_CARD(s -> DesensitizedUtil.idCardNum(s, 3, 4)),

    /**
     * 手机号脱敏
     */
    PHONE(DesensitizedUtil::mobilePhone),

    /**
     * 地址脱敏
     */
    ADDRESS(s -> DesensitizedUtil.address(s, 8)),

    /**
     * 邮箱脱敏
     */
    EMAIL(DesensitizedUtil::email),

    /**
     * 银行卡
     */
    BANK_CARD(DesensitizedUtil::bankCard),

    CLIENT_ID(s -> formToNum(s,8,25)),

    CLIENT_SECRET(s -> formToNum(s,8,25));

    //可自行添加其他脱敏策略

    private final Function<String, String> desensitizer;

    public Function<String, String> desensitizer() {
        return desensitizer;
    }



    public static String formToNum(String str, int front, int end) {
        if (StrUtil.isBlank(str)) {
            return "";
        } else if (front + end > str.length()) {
            return "";
        } else {
            return front >= 0 && end >= 0 ? StrUtil.hide(str, front, str.length() - end) : "";
        }
    }


}
